package com.cloudm.technician.ui.modules.cloudm_box.cloudm_device

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import com.cloudm.technician.ui.modules.cloudm_box.install_check.InstallCheckActivity
import kotlinx.android.synthetic.main.fragment_cloudm_device.*

/**
 * 作者: wesley
 * 时间: 2017/12/25
 * 描述: ui
 */
class CloudmDeviceFragment : BaseFragment<CloudmDevicePresenter>(), CloudmDeviceContract.View {

    private var sn: String? = null
    private var info: DeviceInfo? = null

    override val resource: Int
        get() = R.layout.fragment_cloudm_device

    companion object {
        fun newInstance(sn: String?) = CloudmDeviceFragment().apply {
            arguments = Bundle().apply {
                putString("sn", sn)
            }
        }
    }

    override fun initInject() {
        DaggerCloudmDeviceComponent.builder().appComponent(
                appComponent).cloudmDevicePresenterModule(
                CloudmDevicePresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        sn = bundle.getString("sn")
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {
        presenter?.getDeviceInfo(sn)
    }

    override fun initListener() {

    }

    override fun deviceInfoLoadSuccess(info: DeviceInfo) {
        this.info = info
        tvSnCodeValue.text = info.sn
        tvDeviceNameValue.text = info.deviceName
        tvMachineTypeValue.text = info.type
        tvMachineBrandValue.text = info.brand
        tvMachineModelValue.text = info.model
        tvOwnerNameValue.text = info.nickname
        tvOwnerPhoneValue.text = info.mobile
    }

    override fun deviceInfoLoadFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun showDeviceInfoEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.device_info_is_null))
    }

    override fun finishSelf() {
        val activity = mContext as? CloudmDeviceActivity ?: return
        activity.finish()
    }

    override fun showLoadingMsg() {
        mContext.toast(mContext.resources.getString(R.string.device_info_is_loading))
    }

    override fun openActivity(info: DeviceInfo) {
        val intent = Intent(mContext, InstallCheckActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
            })
        }
        mContext.openActivity(intent)
    }

    fun nextStep() {
        presenter?.openActivity(info)
    }
}